package com.bbtShop.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bbtShop.core.model.Ids;
import com.bbtShop.core.model.Product;
import com.bbtShop.core.model.ProductCategory;
import com.bbtShop.core.model.ProductView;
import com.bbtShop.dao.ProductCategoryMapper;
import com.bbtShop.dao.ProductMapper;
import com.bbtShop.dao.ProductViewMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author : manke
 * @Description
 * @Date: 2020/10/18 19:05
 */
@Service
public class ProductService {


    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductViewMapper productViewMapper;

    public IPage<ProductView> getLisProduct(Integer pageNum, Integer pageSize,
                                        ProductView productView) {
        IPage<ProductView> page = new Page<>(pageNum == null ? 1 : pageNum,
                pageSize == null ? 10 : pageSize);
        LambdaQueryWrapper<ProductView> productLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (productView.getProductName()!=null&& !productView.getProductName().isEmpty()){
            productLambdaQueryWrapper.like(ProductView::getProductName,productView.getProductName());
        }
        if (productView.getCategoryId()!=null){
            productLambdaQueryWrapper.like(ProductView::getCategoryId,productView.getCategoryId());
        }
        return productViewMapper.selectPage(page,productLambdaQueryWrapper );

    }

    public void addUpdateProduct(Product product) {
        if (product.getId() == null) {
            product.setAddTime(new Date());
            product.setUpdTime(new Date());
            productMapper.insert(product);
        } else {
            product.setUpdTime(new Date());
            productMapper.updateById(product);
        }

    }

    public void delProduct(Ids ids) {
        productMapper.deleteBatchIds(ids.getIds());

    }

    public Product getProductInfo(Integer id) {
        return productMapper.selectById(id);

    }

    public List<Product> getAllProductId() {
        return productMapper.selectList(new LambdaQueryWrapper<>());

    }
}
